/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package miselania.almacenamiento;
import java.util.ArrayList;
import java.util.List;
import java.util.*;

/**
 *
 * @author KAREL
 */
public class ejercicios {
  //    public static void main(String args[]) {
//        Scanner in = new Scanner(System.in);
//        
//        List<Atributo> attrs = new ArrayList<Atributo>();
//            attrs.add(new Atributo("character", Character.class));
//            attrs.add(new Atributo("byte", Byte.class));
//            attrs.add(new Atributo("short", Short.class));
//            attrs.add(new Atributo("integer", Integer.class));
//            attrs.add(new Atributo("long", Long.class));
//            attrs.add(new Atributo("float", Float.class));
//            attrs.add(new Atributo("double", Double.class));
//            attrs.add(new Atributo("string", String.class));
//            Relacion rel = new Relacion(attrs);
//  
//            System.out.print("NUMERO DE TUPLAS : ");
//            int nroTuplas = in.nextInt();
//            
//            //List<Comparable> lista = new ArrayList<Comparable>();
//            
//            List<GestorIOTupla> listas = new ArrayList<GestorIOTupla>();
//            List<Tupla> listaTupla = new ArrayList<Tupla>();
//             
//            for (int i = 0; i < nroTuplas; i++) 
//            {   
//                List<Comparable> v = new ArrayList<Comparable>();
//                System.out.print("char: "); 
//                char a = in.next().toCharArray()[0];
//                v.add(new Character(a));
//                System.out.print("byte: "); 
//                v.add(new Byte(in.nextByte()));
//                System.out.print("short: "); 
//                v.add(new Short(in.nextShort()));
//                System.out.print("integer: "); 
//                v.add(new Integer(in.nextInt()));
//                System.out.print("long: "); 
//                v.add(new Long(in.nextLong()));
//                System.out.print("float: ");
//                String flotante = in.next();
//                float numeroflo = Float.parseFloat (flotante);
//                v.add(new Float(numeroflo));
//                System.out.print("double: "); 
//                String doble = in.next();
//                double numerodoble = Double.parseDouble(doble);
//                v.add(new Double(numerodoble));
//                System.out.print("string : "); 
//                v.add(new String(in.next().toString())); 
//                
//                Tupla t = new Tupla(new IdentificadorTupla("redes"+i, i), v);
//                GestorIOTupla man = new GestorIOTupla(rel, "redes"+i);
// 
//                listaTupla.add(t);
//                listas.add(man);
//  
//            }
//            for (int j = 0; j < nroTuplas; j++) {
//                Tupla t = listaTupla.get(j);
//                System.out.println("writing tuple..." + t);
//                byte[] bytes = new byte[1024];
//                GestorIOTupla a  = listas.get(j);
//                a.escribirTupla(t, bytes, j);
//                
//                System.out.println("Mostrar le arreglo de bytes: "+bytes);
//                
//                System.out.println("reading tuple...");
//               
//                Par<Tupla, Integer> st = a.leerTupla(bytes, j);
//                System.out.println(st);
//                System.out.println("...............................................");
//                System.out.println();  
//                
//            }
//        }
//        
    
    
    public static void main(String args[]) {

         try {
            
            List<Atributo> attrs = new ArrayList<Atributo>();
            attrs.add(new Atributo("character", Character.class));
            attrs.add(new Atributo("byte", Byte.class));
            attrs.add(new Atributo("short", Short.class));
            attrs.add(new Atributo("integer", Integer.class));
            attrs.add(new Atributo("long", Long.class));
            attrs.add(new Atributo("float", Float.class));
            attrs.add(new Atributo("double", Double.class));
            attrs.add(new Atributo("string", String.class));
            Relacion rel = new Relacion(attrs);
            
            System.out.print("digite numero de tuplas : ");
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();            
            //System.out.println(n);
            
            int c;
        
            ArrayList ListaP = new ArrayList();
            for(c = 0; c<=n-1 ;c++)
            {
                //String v = "v"+c;
                List<Comparable> vg = new ArrayList<Comparable>();
                //List<Comparable> v2 = new ArrayList<Comparable>();
                System.out.println("tupla Nro "+ c + ":");
                        
                System.out.print("digite caracter : ");
                //char valorchar = in.next().toCharArray(0);   
                char[] st= in.next().toCharArray();
                char valorchar=st[0];
                vg.add(new Character(valorchar));
                
                System.out.print("digite byte : ");
                byte valorbyte = in.nextByte();  
                vg.add(new Byte((byte) valorbyte));
                
                System.out.print("digite short : ");
                short valorshort = in.nextShort();  
                vg.add(new Short((short) valorshort));
                                
                System.out.print("digite entero : ");
                int valorentero = in.nextInt();  
                vg.add(new Integer(valorentero));
                
                System.out.print("digite long : ");
                Long valorlong = in.nextLong();  
                vg.add(new Long(valorlong));
                
                System.out.print("digite float : ");
                float valorfloat = Float.parseFloat(in.next());  
                vg.add(new Float(valorfloat));
                
                System.out.print("digite double : ");
                double valordouble = Double.parseDouble(in.next());  
                vg.add(new Double(valordouble));
                
                //v1.add(new Integer(2048));2
                //v2.add(new Integer(4321));
                
                System.out.print("digite cadena : ");
                String valorcadena = in.next();
                vg.add(new String(valorcadena));
                
                 System.out.println("");
                //v1.add(new String("bla bla"));
                //v2.add(new String("blaa blaa"));
                            
                ListaP.add(vg);            
            }
          
            
            //crear la pagina inicial con identificador y numero de 4k bytes
            Pagina paginai = new Pagina(1,4096);
            GestorPagina GestorP = new GestorPagina(paginai);
            //int p = 1;
            //int ContBytes = 0;
                        
            for(int i = 0; i<=ListaP.size() - 1 ;i++)
            {
                //List v = Arrays.asList(ListaP.get(i)) ;
                                
                Tupla t = new Tupla(new IdentificadorTupla("redes", i), (List)ListaP.get(i));
                GestorIOTupla man = new GestorIOTupla(rel, "redes");

                java.io.RandomAccessFile raf = new java.io.RandomAccessFile("redes", "rw");
                                
                System.out.println("Pagina N° : " + GestorP.SgtPagina);
                System.out.println("writing tuple..." + t);
                byte[] bytes = new byte[1024];

                //man.escribirTupla(t, raf);
                man.escribirTupla(t, bytes, i);
                raf.close();
                System.out.println("mostrar arreglo de bytes" + bytes);

                raf = new java.io.RandomAccessFile("redes", "r");
                System.out.println("reading tuple...");

                // t = man.leerTupla(bytes);
                Par<Tupla, Integer> st = man.leerTupla(bytes, i);
                System.out.println(st);
                raf.close();
                
                // TAREA 3:
                //ContBytes = ContBytes + man.NroBytes();
                //verificar si la pagina actual no es suficiente si es asi usar una nueva
                long d = GestorP.paginaInicial.espaciodisponible( man.byteSize(t));
                GestorP.ControlarPagina(d);
                               
            }
            
            } catch (Exception e) 
            {
                System.err.println("Exception: " + e.getMessage());
                e.printStackTrace(System.err);
            }
        
    } // main()
    
}
